package com.didiglobal.turbo.demo.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.didiglobal.turbo.demo.pojo.po.EmFormTemplate;
import com.didiglobal.turbo.demo.pojo.request.GetFlowModuleListRequest;
import com.didiglobal.turbo.demo.pojo.response.BaseResponse;
import com.didiglobal.turbo.demo.service.EmFormTemplateService;
import com.didiglobal.turbo.demo.util.ReactorNettyHttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;
import reactor.netty.http.client.HttpClient;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author zhanglu
 * @project lu-flow-turbo
 * @description 表单模版
 * @date 2023/3/27 10:56
 */
@RestController
@RequestMapping("/form-template")
public class FormTemplateController {

    private static final Logger LOGGER = LoggerFactory.getLogger(FormTemplateController.class);

    @Resource
    private EmFormTemplateService formTemplateService;

    /**
     * 分页列表
     * @param request
     * @return
     */
    @PostMapping("/page-list")
    public BaseResponse pageList(@RequestBody GetFlowModuleListRequest request){
        return BaseResponse.make(formTemplateService.page(new Page<>(request.getCurrent(), request.getSize()),null));
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public BaseResponse detail(@PathVariable Long id){
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return BaseResponse.make(formTemplateService.getById(id));
    }

    /**
     * 保存
     * @param formTemplate
     * @return
     */
    @PostMapping("/save")
    public BaseResponse save(@RequestBody EmFormTemplate formTemplate){
        return BaseResponse.make(formTemplateService.saveOrUpdate(formTemplate));
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public BaseResponse delete(@PathVariable Long id){
        return BaseResponse.make(formTemplateService.removeById(id));
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @GetMapping("/detail2")
    public BaseResponse detail2(){
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return BaseResponse.make(DateUtil.format(DateUtil.date(), DatePattern.NORM_DATETIME_PATTERN));
    }

    public int t1 = 0;
    public int t2 = 0;
    public int t3 = 0;

    @GetMapping("/t1")
    public String t1(){
        String url = "http://127.0.0.1:8080/form-template/detail2";
        LOGGER.info("t1:" + t1++);
        return HttpRequest.get(url).execute().body();
    }

    @GetMapping("/t2")
    public String t2(){
        String url = "http://127.0.0.1:8080/form-template/detail2";
        String s = ReactorNettyHttpUtils.syncExecute(HttpClient.create().get().uri(url));
        LOGGER.info("t2:" + t2++);
        return s;
    }

    @GetMapping("/t3")
    public DeferredResult t3(){
        String url = "http://127.0.0.1:8080/form-template/detail2";
        DeferredResult<BaseResponse> deferredResult = ReactorNettyHttpUtils.asyncExecute(HttpClient.create().get().uri(url), x -> BaseResponse.make(x));
        LOGGER.info("t3:" + t3++);
        return deferredResult;
    }

}
